package com.xdja.jwt.imagecollect.mvp.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.blankj.utilcode.util.LogUtils
import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.xdja.jwt.imagecollect.R
import com.xdja.jwt.imagecollect.app.adapter.DownloadingAdapter
import com.xdja.jwt.imagecollect.app.db.entity.DownloadFileBean
import com.xdja.jwt.imagecollect.app.utils.DiffDownloadCallBack
import com.xdja.jwt.imagecollect.di.component.DaggerDownloadingComponent
import com.xdja.jwt.imagecollect.di.module.DownloadingModule
import com.xdja.jwt.imagecollect.mvp.contract.DownloadingContract
import com.xdja.jwt.imagecollect.mvp.presenter.DownloadingPresenter
import kotlinx.android.synthetic.main.fragment_downloading.*

/**
 * @author yuanwanli
 * @date 2019/11/11
 * @des 下载中
 */
class DownloadingFragment : BaseFragment<DownloadingPresenter>(), DownloadingContract.View {

    lateinit var downloadingAdapter: DownloadingAdapter
    private var isFirst: Boolean = true
    companion object {
        fun newInstance(): DownloadingFragment {
            val fragment = DownloadingFragment()
            return fragment
        }
    }

    override fun setupFragmentComponent(appComponent: AppComponent) {
        DaggerDownloadingComponent //如找不到该类,请编译一下项目
            .builder()
            .appComponent(appComponent)
            .downloadingModule(DownloadingModule(this))
            .build()
            .inject(this)
    }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_downloading, container, false);
    }

    override fun initData(savedInstanceState: Bundle?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        downloadingAdapter = DownloadingAdapter(null)
        (recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        recyclerView.adapter = downloadingAdapter
        downloadingAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = downloadingAdapter.getItem(position)
            item?.let {
                if (it.hasStart) {
                    mPresenter?.stopDownloadBean(it)
                } else {
                    mPresenter?.downloadBean(it)
                }
            }
        }
        mPresenter?.getAllDownloadingBean()
    }

    override fun showDownloadingBean(beans: List<DownloadFileBean>) {
        LogUtils.e(beans.size)
        if (isFirst) {
            beans.forEach {
                mPresenter?.downloadBean(it)
            }
            isFirst = false
        }
        val callback = DiffDownloadCallBack(beans)
        downloadingAdapter.setNewDiffData(callback)
    }

    override fun setData(data: Any?) {

    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {

    }
}
